package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.CMUtil
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import cn.com.kh.dh.HttpClient
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @projectName: ngsapitask-v3
 * @package: cn.com* @className: chatproxy* @author: LiJT* @description: 项目Token相关统一管理接口* @date: 2022-09-29 13:21
 * @version: 1.0
 */

public class apiCMTemplateList implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiCMTemplateList.class);
    private static String log_Prefix = "自动同步CM模板-";

    private static String CM_ProductToken = "b4b49728-9eda-4f10-980e-da21ac4372b7";
    private static String CM_AccountID = "154d3c2e-472c-40df-a0e9-597647da4069";
    private static String CM_ChannelRequestId = "f47f65e1-52dd-4607-818a-67ec91c658ff";

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        // 调用IP过滤
        if (KHDBConfig.getParam(eliteBean, "#APIIP") == "1" && !KHUtil.getRemoteHost(request)) {
            log.warn(log_Prefix + "API被非法IP调用-Request Not Allow!");
            return "Request Not Allow!";
        }

        // 获取数据源
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        String Syncpram = "";

        String getMethod = request.getMethod();
        if (getMethod.equals("GET")) {
            Syncpram = request.getQueryString();
            log.info("接收调用GET参数信息：" + Syncpram);
        } else if (getMethod.equals("POST")) {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                Syncpram += line;
            }

            log.info("接收调用POST报文信息：" + System.getProperty("line.separator") + Syncpram);
        }

        JSONObject resultBody = new JSONObject();
        resultBody.put("code", "1");
        resultBody.put("msg", "调用成功");

        try {
            // 解析请求报文
            JSONObject callJson = JSONObject.fromObject(Syncpram);

            String call_sql = callJson.getString("call_sql");
            log.debug(log_Prefix + "代理API基础信息:call_sql：" + call_sql);

            List call_sql_list = eliteBean.chaxun_sql(call_sql, "dataSource");
            List<Map<String, Object>> selectRouteResult = jt.queryForList(call_sql);
            log.debug("代理API基础信息 Result:" + selectRouteResult.toString());

            JSONArray resData = Util.getJsonArrayFromList(call_sql_list);

            resultBody.put("data", resData);

        } catch (Exception e) {
            log.error("接口处理异常", e);
            // 失败返回
            resultBody.put("code", "-1");
            resultBody.put("msg", "调用代理异常：" + e.getMessage());

        }

        log.debug("Proxy处理完成返回结果:" + resultBody.toString());

        return resultBody.toString();
    }

}
